package cn.pku.ss.fa.ui.component;

import java.util.List;
import javax.naming.InitialContext;
import org.zkoss.zul.Window;
import cn.pku.ss.fa.bean.FriendLittleBean;
import cn.pku.ss.fa.sessionbean.UserManagerLocal;

/**
 * @author SirX
 */
public class SearchFriendsResultComponent extends Window {
	
	public List<FriendLittleBean> getFriendsByName(String name) {
		try {
			InitialContext ctx = new InitialContext();
			UserManagerLocal userDao = (UserManagerLocal)ctx.lookup("FriendsAnywhere/UserManager/local");
			return userDao.getFriendsByName(name);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
//		LinkedList<FriendLittleBean> result = new LinkedList<FriendLittleBean>();
//		for(int i=0; i<50; i++) {
//			FriendLittleBean bean = new FriendLittleBean();
//			bean.setId(i);
//			bean.setName("name"+i);
//			bean.setCity("city"+i);
//			bean.setProvince("province"+i);
//			bean.setUniversityName("universityName"+i);
//			bean.setStatus(i+"statusssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
//			bean.setImageURL("/image/status.png");
//			bean.setPageURL("http://www.g.cn");
//			result.add(bean);
//		}
//		return result;
	}
	
	public boolean makeFriends(int userId, int friendId) {
		if( userId==friendId ) {
			return false;
		}
		try {
			InitialContext ctx = new InitialContext();
			UserManagerLocal userDao = (UserManagerLocal)ctx.lookup("FriendsAnywhere/UserManager/local");
			userDao.makeFriends(userId, friendId);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean isFriendship(int userId1, int userId2) {
		if( userId1==userId2 ) {
			return true;
		}
		try {
			InitialContext ctx = new InitialContext();
			UserManagerLocal userDao = (UserManagerLocal)ctx.lookup("FriendsAnywhere/UserManager/local");
			return userDao.isFriendship(userId1, userId2);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
